import defaultSettings from '@/settings'
import { getSysConfig } from '@/api/system/config';

const { tagsView, sidebarLogo, showSettings, fixedHeader } = defaultSettings
const state = {
    // 是否显示多标签页
    tags_view: tagsView,
    // 是否在右面板显示设置
    showSettings: showSettings,
    // 是否固定头部标签
    fixedHeader: fixedHeader,
    // 是否显示侧边栏logo
    sidebarLogo: sidebarLogo,
    // 系统配置信息
    siteData: {}
}

const mutations = {
    CHANGE_SETTING: (state, { key, value }) => {
        // state.hasOwnProperty(key)
        if (Object.hasOwnProperty.call(state, key)) {
            state[key] = value
        }
    },
    SET_SITE_DATA: (state, siteData) => {
        state.siteData = siteData
    }

}

const actions = {
    // 修改某一项配置 key:value
    changeSetting ({ commit }, data) {
        commit('CHANGE_SETTING', data)
    },
    // 获取网站配置信息
    getSite ({ commit }) {
        return new Promise((resolve, reject) => {
            // console.log('++++++++++++++++++++++++++++++')
            getSysConfig(1).then(res => {
                if (res.code === 200) {
                    commit('SET_SITE_DATA', res.data)
                    resolve(res.data)
                }
            }).catch(error => {
                reject(error)
            })
        })
    }
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}
